package lyh;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * 功能
 * 作者：lyh
 * 日期：2024/7/12 14:05
 */
public class test0712 {
    private static Scanner scanner = new Scanner(System.in);
    private static ArrayList<Student> stuList = new ArrayList<>();
    private static FileWriter fileWriter2;

    static {
        try {
            fileWriter2 = new FileWriter("stu0712.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static BufferedWriter stuwriter = new BufferedWriter(fileWriter2);
    public static void main(String[] args) throws IOException {
        HashMap<String, String> admMap = new HashMap<>();

        admMap.put("张三丰","123456");
        admMap.put("李四光","777777");
        admMap.put("王老五","888888");
        FileWriter fileWriter = new FileWriter("管理员0712.txt");
        BufferedWriter admwriter = new BufferedWriter(fileWriter);
        Set<String> keySets = admMap.keySet();
        for (String keySet : keySets) {
            admwriter.write(keySet+"#"+admMap.get(keySet));
            admwriter.newLine();
        }
        admwriter.close();

        while (true){
            System.out.println("请输入用户名:");
            String username = scanner.next();
            if (admMap.containsKey(username)){
                System.out.println("请输入密码：");
                String password = scanner.next();
                if (admMap.get(username).equals(password)){
                    come();
                    stuManager(username);
                }else {
                    System.out.println("用户名密码错误！");
                }
            }else {
                System.out.println("该系统没有此人信息！");
            }
        }
    }

    public static void come(){
        for (int i = 0; i < 2; i++) {
            System.out.println("请输入第"+(i+1)+"个学生的姓名：");
            String name = scanner.next();
            System.out.println("请输入第"+(i+1)+"个学生的年龄：");
            int age = scanner.nextInt();
            System.out.println("请输入第"+(i+1)+"个学生的性别：");
            String sex = scanner.next();
            System.out.println("请输入第"+(i+1)+"个学生的成绩：");
            int score = scanner.nextInt();
            stuList.add(new Student(name,age,sex,score));
        }
        System.out.println("学生信息录入完毕！");
    }

    public static void stuManager(String thename) throws IOException {
        while (true){
            System.out.println("**********欢迎"+thename+"进入学生管理系统**********");
            System.out.println("1.查询");
            System.out.println("2.查找");
            System.out.println("3.新增");
            System.out.println("4.修改");
            System.out.println("5.删除");
            System.out.println("6.退出");
            System.out.println("请选择...");

            int choice = scanner.nextInt();
            switch (choice){
                case 1:
                    for (Student student : stuList) {
                        System.out.println(student);
                    }
                    System.out.println("学生信息加载完毕！");
                    break;
                case 2:
                    System.out.println("请输入你要查找学生的学号：");
                    int id = scanner.nextInt();
                    for (Student student : stuList) {
                        if (student.getId()==id){
                            System.out.println(student);
                        }
                    }
                    System.out.println("学生信息查找成功！");
                    break;
                case 3:
                    System.out.println("请输入学生的姓名：");
                    String name = scanner.next();
                    System.out.println("请输入学生的年龄：");
                    int age = scanner.nextInt();
                    System.out.println("请输入学生的性别：");
                    String sex = scanner.next();
                    System.out.println("请输入学生的成绩：");
                    int score = scanner.nextInt();
                    stuList.add(new Student(name,age,sex,score));
                    System.out.println("学生信息添加完毕！");
                    break;
                case 4:
                    System.out.println("请输入你要修改学生的学号：");
                    int id1 = scanner.nextInt();
                    for (Student student : stuList) {
                        if (student.getId()==id1){
                            System.out.println("请输入学生的姓名：");
                            String name2 = scanner.next();
                            student.setName(name2);
                            System.out.println("请输入学生的年龄：");
                            int age2 = scanner.nextInt();
                            student.setAge(age2);
                            System.out.println("请输入学生的性别：");
                            String sex2 = scanner.next();
                            student.setSex(sex2);
                            System.out.println("请输入学生的成绩：");
                            int score2 = scanner.nextInt();
                            student.setScore(score2);
                        }
                    }
                    System.out.println("学生信息修改完毕！");
                    break;
                case 5:
                    System.out.println("请输入你要删除学生的学号：");
                    int id2 = scanner.nextInt();
                    Iterator<Student> iterator = stuList.iterator();
                    while (iterator.hasNext()){
                        Student student = iterator.next();
                        if (student.getId()==id2){
                            iterator.remove();
                        }
                    }
                    System.out.println("学生信息删除成功！");
                    break;
                case 6:
                    Collections.sort(stuList, new Comparator<Student>() {
                        @Override
                        public int compare(Student u1, Student u2) {
                            int result = u2.getScore()-u1.getScore();
                            if (result==0){
                                result = u1.getName().charAt(0)-u2.getName().charAt(0);
                            }
                            return result;
                        }
                    });
                    System.out.println("退出之前打印学生成绩：");
                    for (Student student : stuList) {
                        System.out.println(student.getId()+"--"+student.getName()+"--"+student.getAge()+
                                "--"+student.getSex()+"--"+student.getScore());
                        stuwriter.write(student.getId()+"--"+student.getName()+"--"+student.getAge()+
                                "--"+student.getSex()+"--"+student.getScore());
                        stuwriter.newLine();
                    }
                    stuwriter.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("请输入正确的选项(1-6)：");
            }
        }
    }
}
